<script type="text/javascript">
    function edit(id)
    {
        var form = document.getElementById("form");
        form.name.value = document.getElementById("name" + id).innerHTML;
        form.weight.value = document.getElementById("weight" + id).innerHTML;
        form.itemid.value = id;
        var btnEdit = document.getElementById('btnEdit');
        var btnAdd = document.getElementById('btnAdd')
        var btnCancel = document.getElementById('btnCancel');
        btnEdit.disabled = false;
        btnAdd.style.visibility = 'hidden';
        btnCancel.style.visibility = 'visible';
        var subjectList = document.getElementById('subjectList');
        subjectList.disabled = true;
    }

    function submitAdd(subjectid)
    {
        var form = document.getElementById("form");
        //form.action += "index/" + subjectid;
        form.actiontype.value = "add";
        form.submit();
    }

    function submitEdit(subjectid)
    {
        var form = document.getElementById("form");
        //form.action += "index/" + subjectid;
        form.actiontype.value = "edit";
        form.submit();
    }


    function remove(id)
    {
        var r=confirm("Bạn có muốn xóa không?");
        if (r==true)
            {
                var form=document.getElementById("form");
                form.itemid.value=id;
                form.actiontype.value="remove";
                form.submit();

            }
    }

    function changeClass()
    {
        var subjectList = document.getElementById('subjectList');
        var location = "<?php echo Utility::getSiteUrl();?>/sections/index/" + subjectList.value;
        window.location = "<?php echo Utility::getSiteUrl();?>/sections/index/" + subjectList.value;

    }

    function cancel()
    {
        var btnEdit = document.getElementById('btnEdit');
        var btnAdd = document.getElementById('btnAdd')
        var btnCancel = document.getElementById('btnCancel');
        btnCancel.style.visibility = 'hidden';
        btnAdd.style.visibility = 'visible';
        var subjectList = document.getElementById('subjectList');
        subjectList.disabled = false;
    }
</script>

<div id="main">
    <form  id="form" action="<?php echo Utility::getSiteUrl();?>/sections/index/<?php echo $currentSubject; ?>" method ="post">
        <input type="hidden" name="actiontype" value="" />
        <input type="hidden" name="subjectid" value=""/>
        <input type="hidden" name="itemid"/><br/>
        <input type="hidden" name="classid"/><br/>
        <fieldset>
            <legend>Thông tin môn học</legend>
            <table>
                <tr>
                    <td>Mã môn học</td>
                    <td>
                        <select name="subjectList" id="subjectList" onchange="changeClass();">
                            <option>Chọn môn học</option>
                            <?php
                                $size = $subjectList->getSize();
                                if($size > 0)
                                {
                                    for($i = 0; $i < $size; $i++)
                                    {
                                        $row = $subjectList->getRow($i);
                                        ?>
                                        <option value="<?php echo $row->id;?>" <?php if($row->id == $currentSubject) echo "selected='selected'" ?> ><?php echo $row->name;?></option>
                                        <?php
                                    }
                                }
                            ?>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>Tên chương</td>
                    <td><input type="text" name="name"  <?php if($currentSubject == NULL) echo "disabled='disabled'";?>/></td>
                </tr>
                <tr>
                    <td>Trọng số</td>
                    <td><select name="weight"  <?php if($currentSubject == NULL) echo "disabled='disabled'";?>>
                        <option value="1">1</option>
                        <option value="2">2</option>
                        <option value="3">3</option>
                        <option value="4">4</option>
                        <option value="5">5</option>
                        <option value="6">6</option>
                        <option value="7">7</option>
                        <option value="8">8</option>
                        <option value="9">9</option>
                        <option value="10">10</option>
                    </select></td>
                </tr>
                <tr>
                    <td colspan="2">
                        <input type="button" id="btnAdd" value="Thêm" onclick="submitAdd('<?php echo $currentSubject;?>')" <?php if($currentSubject == NULL) echo "disabled='disabled'";?>/>
                        <input type="button" id="btnEdit" disabled="disabled" value="Cập nhật" onclick="submitEdit('<?php echo $currentSubject; ?>')"/>
                        <input type="button" id="btnCancel" value="Bỏ qua" onclick="cancel()" style="visibility:hidden;" />
                    </td>
                </tr>
            </table>
        </fieldset>

        &nbsp;&nbsp;&nbsp;

         
        
        <br/>

        <table border="1">
            <thead>
                <tr>
                    <th width="20"> STT </th>
                    <th width="60">Mã môn học</th>
                    <th width="200">Tên chương</th>
                    <th width="20">Trọng số</th>
                    <th colspan="2"></th>
                </tr>
            </thead>
            <tbody>
                <?php
                if(isset($sectionList))
                {
                    $size = $sectionList->getSize();
                    if($size > 0)
                    {
                        for($i = 0; $i < $size; $i++)
                        {
                            $section = $sectionList->getRow($i);
                            ?>
                    <tr>
                        <td><?php echo $i + 1; ?></td>
                        <td id="subject<?php echo $section->id; ?>"><?php echo $section->subjectid; ?></td>
                        <td id="name<?php echo $section->id; ?>"><?php  echo $section->name ?></td>
                        <td id="weight<?php echo $section->id; ?>"><?php echo $section->weight ?></td>
                        <td><input type="button" value="Sửa" onclick="edit(<?php echo $section->id; ?>)"/></td>
                        <td><input type="button" value="Xóa" onclick="remove(<?php echo $section->id; ?>)"/></td>
                    </tr>
                            <?php
                        }
                    }
                    else
                    {
                        ?>
                        <tr>
                            <td colspan="6">Chưa có dữ liệu</td>
                        </tr>
                        <?php
                    }
               }
               else
               {

               }
                ?>
            </tbody>
        </table>
        

    </form>
    




</div>